//
//  CircleOrbitManager.m
//  Project 5
//
//  Created by vbnn2 on 6/20/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "CircleOrbitManager.h"
#import "CircleOrbit.h"


@implementation CircleOrbitManager

-(id) initWithListCircleOrbit:(NSArray *)listCircleOrbit
{
    if (self = [super init])
    {
        for (int i = 0; i < listCircleOrbit.count; i++)
        {
            CircleOrbit *circleOrbit = (CircleOrbit *)[listCircleOrbit objectAtIndex:i];
            [self addChild:circleOrbit z:0 tag:i];
        }
        [self getListAvailableColor];
        [self scheduleUpdate]; // retain count increase 1 here
    }
    return self;
}

-(void) setListOrbitRadius:(NSArray *)listOrbitRadius
{
    mListOrbitRadius = [listOrbitRadius retain];
    
    LEVEL_MODE mode = [LevelManager shareLevelManager]._mode;
    if (mode == MODE_NORMAL)
    {
        [self initRadiusNormal];
    }
    else if (mode == MODE_SAVE_PLANET)
    {
        [self initRadiusSavePlanet];
    }
}

-(void) initRadiusNormal
{
    for (int i = 0; i < self.children.count; i++)
    {
        CircleOrbit *circleOrbit = (CircleOrbit *)[self getChildByTag:i];
        
        if (i >= mListOrbitRadius.count)
        {
            float radius = [[mListOrbitRadius objectAtIndex:(mListOrbitRadius.count - 1)] floatValue];
            float lastRadius = [[mListOrbitRadius objectAtIndex:(mListOrbitRadius.count - 2)] floatValue];
            
            [circleOrbit setRadius:(radius * 2 - lastRadius)];
            [circleOrbit setVisible:NO];
            [circleOrbit setCheckCollision:NO];
        }
        else
        {
            float radius = [[mListOrbitRadius objectAtIndex:i] floatValue];
            [circleOrbit setRadius:radius];
            [circleOrbit setCheckCollision:YES];
        }
    }
}

-(void) initRadiusSavePlanet
{
    float radius = [[mListOrbitRadius objectAtIndex:0] floatValue];
    float dzRadius = [[mListOrbitRadius objectAtIndex:2] floatValue];
    
    for (int i = 0; i < self.children.count; i++)
    {
        CircleOrbit *circleOrbit = (CircleOrbit *)[self getChildByTag:i];
        
        [circleOrbit setVisible:NO];
        [circleOrbit setCheckCollision:NO];
        [circleOrbit setRadius:radius];
        [circleOrbit setNextRadius:dzRadius];
        radius += [[mListOrbitRadius objectAtIndex:1] floatValue];
    }
}

-(void) update:(ccTime)delta
{
    mLastNumAlive = [self getNumCircleOrbitAlive];
    BOOL isChanged = NO;
    for (int i = 0; i < self.children.count; i++)
    {
        CircleOrbit *circleOrbit = (CircleOrbit *)[self getChildByTag:i];
        if ([circleOrbit getState] == STATE_DESTROYING)
        {
            [circleOrbit runEffect:EFFECT_DISAPPEAR];
            CCLOG(@"orbit destroyed");
            isChanged = YES;
        }
    }
    
    LEVEL_MODE mode = [LevelManager shareLevelManager]._mode;
    if (mode == MODE_NORMAL)
    {
        if (isChanged)
            [self updateNormal];
    }
    else if (mode == MODE_SAVE_PLANET)
    {
        [self updateSavePlanet];
    }
}

-(void) updateNormal
{
    int count = 0;
    int numAlive = [self getNumCircleOrbitAlive];
    
    // check for update radius
    if (mLastNumAlive <= mListOrbitRadius.count && numAlive <= mListOrbitRadius.count)
        return;
    
    if (numAlive < mListOrbitRadius.count)
        count = mListOrbitRadius.count - numAlive;
    
    for (int i = 0; i < self.children.count; i++)
    {
        CircleOrbit *circleOrbit = (CircleOrbit *)[self getChildByTag:i];
        if ([circleOrbit getState] == STATE_DESTROYED)
            continue;
        else if ([circleOrbit getState] == STATE_ACTIVE)
        {
            NSNumber *radius = [mListOrbitRadius objectAtIndex:count];
            [circleOrbit setNextRadius:[radius floatValue]];
            
            if (circleOrbit.visible == NO)
            {
                [circleOrbit setVisible:YES];
                [circleOrbit setCheckCollision:YES];
                [circleOrbit runEffect:EFFECT_APPEAR];
            }
            
            count++;
            if (count >= mListOrbitRadius.count)
                break;
        }
    }
}

-(void) updateSavePlanet
{
    float radius = [[mListOrbitRadius objectAtIndex:0] floatValue];
    float dzRadius = [[mListOrbitRadius objectAtIndex:2] floatValue];
    for (int i = 0; i < self.children.count; i++)
    {
        CircleOrbit *circleOrbit = (CircleOrbit *)[self getChildByTag:i];
        if (circleOrbit._radius <= radius && circleOrbit._state == STATE_ACTIVE && circleOrbit.visible == NO)
        {
            [circleOrbit setVisible:YES];
            [circleOrbit setCheckCollision:YES];
            [circleOrbit runEffect:EFFECT_APPEAR];
        }
        else if (circleOrbit._radius <= dzRadius && circleOrbit._state == STATE_ACTIVE)
        {
            circleOrbit._state = STATE_DESTROYED;
            [circleOrbit runEffect:EFFECT_DISAPPEAR];
        }
    }
}

-(int) getNumCircleOrbitAlive
{
    int count = 0;
    for (int i = 0; i < self.children.count; i++)
    {
        CircleOrbit *circleOrbit = (CircleOrbit *)[self getChildByTag:i];
        if (circleOrbit._state == STATE_ACTIVE)
            count++;
    }
    
    return  count;
}

-(BOOL) isAllCircleDestroy
{
    for (int i = 0; i < self.children.count; i++)
    {
        CircleOrbit *circleOrbit = (CircleOrbit *)[self getChildByTag:i];
        if ([circleOrbit getState] != STATE_DESTROYED)
        {
            return NO;
        }
    }
    return YES;
}

-(NSArray *) getListAvailableColor
{
    NSMutableArray *listAvailableColor = [[[NSMutableArray alloc] init] autorelease];
    
    for (int i = 0; i < self.children.count; i++)
    {
        CircleOrbit *circleOrbit = (CircleOrbit *)[self getChildByTag:i];
        for (int j = 0; j < [circleOrbit getNumCircle]; j++)
        {
            Circle *circle = [circleOrbit getCircleAtIndex:j];
            if ([circle isDestroy]) continue;
            
            CircleSegment *circleSegment = [circle getCurrentSegment];
            if (circleSegment._type == TYPE_COLOR)
            {
                if (![listAvailableColor containsObject:[NSNumber numberWithInt:circleSegment._color]])
                {
                    [listAvailableColor addObject:[NSNumber numberWithInt:circleSegment._color]];
                    CCLOG(@"--- contain color: %d", circleSegment._color);
                }
            }
        }
    }
    
    return listAvailableColor;
}

-(void) dealloc
{
    [super dealloc];
    [self unscheduleUpdate];
    [mListOrbitRadius release];
    mListOrbitRadius = nil;
}

@end
